package com.ruoyi.project.digital.controller.api;


import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.aspectj.lang.annotation.NoNeedAccess;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.common.component.WebSocketServer;
import com.ruoyi.project.digital.domain.*;
import com.ruoyi.project.digital.domain.dto.LoginDto;
import com.ruoyi.project.digital.domain.dto.LoginWechatUser;
import com.ruoyi.project.digital.domain.dto.MessageDto;
import com.ruoyi.project.digital.domain.vo.*;
import com.ruoyi.project.digital.service.*;
import com.ruoyi.project.digital.service.impl.VoteServiceImpl;
import com.ruoyi.project.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;


@Api(value = "首页相关接口", tags = "首页相关接口")
@RestController
@RequestMapping("api/front/message")
public class MessageSocketController extends BaseController {


    @Autowired
    private WechatUserTokenService wechatUserTokenService;

    @Autowired
    private IUserTaskService userTaskService;
    @Autowired
    private IPetMessageService petMessageService;
    @Autowired
    private IPetMessageDetailsService petMessageDetailsService;
    @Autowired
    private IPhrasesService phrasesService;
    @Autowired
    private IUserBackService userBackService;

    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IPetService petService;



    @ApiOperation(value = "创建回话")
    @GetMapping("/creatMessge/{petId}")
    private AjaxResult creatMessge(@PathVariable Long petId) {

        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        if(null!=loginMember){
         Long messageId=   petMessageService.creatMessge(loginMember.getId(),petId);
            return AjaxResult.success(messageId);
        }

        return AjaxResult.error();
    }

    @ApiOperation(value = "发送消息")
    @GetMapping("/sendMessge")
    private AjaxResult sendMessge(MessageDto dto) {

        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        if(null!=loginMember){
            PetMessageDetails details= null;
            try {
                details = petMessageDetailsService.sendMessge(dto);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return AjaxResult.success(details.getContent());
        }

        return AjaxResult.error();
    }
    @ApiOperation(value = "与宠物的最近一次对话")
    @GetMapping("/nearMessageList")
    private AjaxResult<List<PetMessageDetailsVO>> nearMessageList() {

        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        List<PetMessageDetailsVO> detailsVOList=petMessageDetailsService.nearMessageList(loginMember.getId());
        return AjaxResult.success(detailsVOList);
    }
    @ApiOperation(value = "对话列表")
    @GetMapping("/messageList")
    private TableDataInfo<List<PetMessageVO>> messageList(String keyWord, String times) {
        startPage();
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        List<PetMessageVO> detailsVOList=petMessageService.messageList(loginMember.getId(),keyWord, times);
        return getDataTable(detailsVOList);
    }
    @ApiOperation(value = "根据回话id来查询聊天详情")
    @GetMapping("/messageDtails/{id}")
    private AjaxResult<List<PetMessageDetailsVO>> messageDtails(@PathVariable Long id) {
        List<PetMessageDetailsVO> detailsVOList=petMessageDetailsService.messageDtails(id);
        return AjaxResult.success(detailsVOList);
    }



    @ApiOperation(value = "喜欢该聊天")
    @GetMapping("/likeMessage/{id}")
    private AjaxResult likeMessage(@PathVariable Long id) {
        petMessageDetailsService.likeMessage(id);

        return AjaxResult.success();
    }


    @ApiOperation(value = "不喜欢该聊天")
    @GetMapping("/noLikeMessage/{id}")
    private AjaxResult noLikeMessage(@PathVariable Long id) {
        petMessageDetailsService.noLikeMessage(id);
        return AjaxResult.success();
    }

    @ApiOperation(value = "任务列表")
    @GetMapping("/taskDay")
    private AjaxResult<List<UserTaskVO>>  taskDay(String type) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
       List<UserTaskVO> userTaskVOList= userTaskService.taskDay(loginMember.getId(),type);
        return AjaxResult.success(userTaskVOList);
    }

    @ApiOperation(value = "任务时间线")
    @GetMapping("/taskTimeLine")
    private AjaxResult<List<UserTask>>  taskTimeLine() {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        List<UserTask> list= userTaskService.taskTimeLine(loginMember.getId());
        return AjaxResult.success(list);
    }

    /*@ApiOperation(value = "游戏任务")
    @GetMapping("/taskGame")
    private AjaxResult<List<UserTaskVO>> taskGame() {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        List<UserTaskVO> userTaskVOList= userTaskService.taskGame(loginMember.getId());
        return AjaxResult.success(userTaskVOList);
    }*/
    @ApiOperation(value = "完成任务")
    @GetMapping("/finishTask/{taskId}")
    private AjaxResult finishTask(@PathVariable Long taskId) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        userTaskService.finishTask(loginMember.getId(),taskId);
        return AjaxResult.success();
    }

    @GetMapping("/phrases")
    private AjaxResult phrases() {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        List<Phrases> phrasesList=  phrasesService.selectFrontList(loginMember.getId());
        return AjaxResult.success(phrasesList);
    }


    @GetMapping("/getPetModel/{id}")
    private AjaxResult petModel(@PathVariable Long id) {

        return AjaxResult.success(petService.getById(id));
    }
    @ApiOperation(value = "获取当前用户默认的聊天背景")
    @GetMapping("/deafultBackUrl/{messageId}")
    private AjaxResult deafultBackUrl(@PathVariable Long messageId) {
        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TBack back=userBackService.selectFrontBack(loginMember.getId(),messageId);
        return AjaxResult.success(back);
    }

    @ApiOperation(value = "更换当前聊天的背景")
    @GetMapping("/changBack/{messageId}")
    private AjaxResult changBack(@PathVariable Long  messageId) {

        LoginWechatUser loginMember = wechatUserTokenService.getLoginWechatUser(ServletUtils.getRequest());
        TBack back=userBackService.changBack(loginMember.getId(),messageId);
        return AjaxResult.success(back);
    }

    @ApiOperation(value = "获取分享地址")
    @GetMapping("/share/{key}")
    private AjaxResult changBack(@PathVariable String key) {
        switch (key){
            case "1":
                return AjaxResult.success(sysConfigService.selectConfigByKey("system_shara_url"));
            case "2":
                return AjaxResult.success(sysConfigService.selectConfigByKey("system_shara_client_img"));
            case "3":
                return AjaxResult.success(sysConfigService.selectConfigByKey("system_shara_association_img"));
        }
        return AjaxResult.success(sysConfigService.selectConfigByKey("system_shara_url"));
    }


}

